package com.haiqiu.tools.auth.utils;

/**
 * 提供加解密后 byte[]-->String 以及 String-->byte[]的转换工具类
 * Created by LGC on 2017/5/7.
 */

public class HexUtils {
    private final static String HEX = "0123456789ABCDEF";
    /**
     * 二进制转(16进制)字符串
     */
    public static String ByteToHex(byte[] bytesKey) {
        if (bytesKey == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : bytesKey) {
            sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
        }
        return sb.toString();
    }

    /**
     * (16进制)字符转二进制
     */
    public static byte[] HexToByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
        }
        return result;
    }
}